<template>
	<view>
		<!-- 搜索页面 -->
		<u-navbar :height="55">
			<view class="w100 p_4 position_r">
				<tabbar-search @change="onSearchChange" @focus="onFocus" @blur="onBlur" @search="onSearch"></tabbar-search>
			</view>
			
			<!-- 搜索提示 -->
			<view class="search-suggest-box" v-show="showSuggest && searchWord">
				<view @tap.stop="onClickSearch(searchWord)" class="searchWordColor" v-if="searchWord">搜索"{{ searchWord }}"</view>
				
				<u-cell-item v-for="item in searchSuggestList" icon="search" @click="onClickSearch(item.keyword)" :arrow="false" :title="item.keyword"></u-cell-item>
			</view>
			
		</u-navbar>
		
		<view class="content">
			<!-- 热搜 -->
			<u-cell-item :arrow="false">
				<view slot="title" class="hotSearch" >热搜榜</view>
			</u-cell-item>
			
			<u-divider></u-divider>
			
			<view class="hot-item-box">
				<u-row gutter="16" justify="bewteen" class="w100 overflow_h">
					<u-col :span="6" v-for="(item, index) in searchHotDetailList" @click="onClickSearch(item.searchWord)">
						<view class="hot-item">
							<view class="number" :class="{'red-text': index < 3}">{{index + 1}}</view>
							<view class="word lin-1" :class="{'text-bolder' : index < 3}">{{item.searchWord}}</view>
							<image v-show="item.iconUrl" class="ml_3 hotImage" mode="widthFix" :src="item.iconUrl"></image>
						</view>
					</u-col>
				</u-row>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				searchHotDetailList: [],	// 热搜列表
				showSuggest: false,			// 显示搜索提示
				searchWord: '',				// 搜索值
				searchSuggestList: []		// 搜索建议
				
				
				
				
			}
		},
		created() {
			this.init()
		},
		methods: {
			init() {
				// 获取 默认搜索关键词
				this.$api.searchDefault().then(res => {
					this.defaultSearchWord = res.data.showKeyword
				})
				
				// 获取 热搜列表(详细)
				this.$api.searchHotDetail().then(res => {
					this.searchHotDetailList = res.data
				})
			},
			
			/* 搜索 */
			onClickSearch(word) {
				console.log(word)
				// 去到搜索结果的页面
				if(!word){
					return
				}
				console.log("跳转")
				uni.navigateTo({
					url: '/pages/searchPage/searchResult?word=' + encodeURIComponent(word)
				})
			},
			
			/* 正在输入内容 */
			onSearchChange(value) {
				this.searchWord = value
				if(!value){
					return
				}
				// type : 如果传 'mobile' 则返回移动端数据
				this.$api.searchSuggest({data: {keywords: value,type:'mobile'}})
				.then(res => {
					console.log(res)
					if(res.result){
						this.searchSuggestList = res.result.allMatch
						console.log(this.searchSuggestList)
					}
				})
			},
			/* 输入框获得焦点时触发 */
			onFocus(value) {
				this.showSuggest = true;
			},
			/* 输入框失去焦点时触发 */
			onBlur(value){
				setTimeout(()=>{
					this.showSuggest = false;
				}, 100);
			},
			/* 确定搜索 */
			onSearch(value) {
				this.onClickSearch(value)
			}
			
			
			
			
			
		}
	}
</script>

<style scoped>
	.position_r{
		position: relative;
	}
	.content{
		background-color: #fff;
	}
	.hotSearch{
		font-weight: bolder;
		font-size: 30rpx;
	}
	.overflow_h{
		overflow: hidden;
	}
	.hotImage{
		width: 30rpx;
		max-height: 40rpx;
	}
	.search-suggest-box{
		width: 100%;
		padding: 20rpx;
		position: absolute;
		top: 100rpx;
		left: 0;
		background-color: #fff;
		border: 1px solid #ddd;
		border-radius: 10rpx;
	}
	.searchWordColor{
		color: #2979FF;
	}
	
	/* 热搜l列表 */
	.hot-item-box {
		padding: 20rpx;
	}
	
	.hot-item-box .hot-item {
		margin-bottom: 30rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-items: center;
	}
	
	.hot-item-box .hot-item .number{
		width: 50rpx;
	}
	
	.hot-item-box .hot-item .red-text{
		color: red;
		font-weight: bolder;
	}
	
	.hot-item-box .hot-item .text-bolder{
		font-weight: bolder;
	}
	
	.hot-item-box .hot-item .word{
		
	}

</style>
